package com.ruoyi.api.dailyWork.controller;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.result.ResultEnum;
import com.ruoyi.result.ResultView;
import com.ruoyi.util.HttpClientUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

/**
 * Author: LvKinfy
 * Time: 18:23
 * Desc: 工作汇报模板
 */
@RestController
@RequestMapping("/api/worktemplete")
public class WorkTempleteController {
  @Value("${wspaas.dailyworkUrl}")
  private String dwUrl;

  /**
   * 获取系统模板(如日报，车辆维修，物资申领)
   *
   * @param type daily,apply
   */
  @PostMapping("/sys/{type}/list")
  private String getSysTemplete(@PathVariable String type) {
    StringBuilder sb = new StringBuilder();
    sb.append(dwUrl).append("/api/systemplete/list/").append(type);
    return HttpClientUtil.doGet(sb.toString());
  }

  //创建系统模板
  @PostMapping("/sys/add")
  private String addSysTemp(@RequestBody JSONObject o) {
    StringBuilder sb = new StringBuilder();
    sb.append(dwUrl).append("/api/systemplete/commit");
    return HttpClientUtil.doPost(sb.toString(), o);
  }

  //删除系统模板
  @PostMapping("/sys/del/{id}")
  private String delSysTemp(@PathVariable String id) {
    StringBuilder sb = new StringBuilder();
    sb.append(dwUrl).append("/api/systemplete/del/").append(id);
    return HttpClientUtil.doGet(sb.toString());
  }

  //更新系统模板
  @PostMapping("/sys/update")
  private Object updateSysTemp(@RequestBody JSONObject o) {
    if (null==o||null == o.get("id")) {
      return ResultView.error(ResultEnum.CODE_2);
    }
    StringBuilder sb = new StringBuilder();
    sb.append(dwUrl).append("/api/systemplete/update/").append(o.get("id"));
    return HttpClientUtil.doPost(sb.toString(), o);
  }

  //获取系统模板详情
  @PostMapping("/sys/detail/{id}")
  private String getSysTemp(@PathVariable String id) {
    StringBuilder sb = new StringBuilder();
    sb.append(dwUrl).append("/api/systemplete/get/").append(id);
    return HttpClientUtil.doGet(sb.toString());
  }

  /**
   * 获取用户收藏的模板(如日报，车辆维修，物资申领)
   *
   * @param type dailywork,apply
   * @param uid
   */
  @PostMapping("/usr/{type}/list/{uid}")
  private String getTemplete(@PathVariable String type, @PathVariable String uid) {
    StringBuilder sb = new StringBuilder();
    sb.append(dwUrl).append("/api/usrtemplete/list/").append(type).append("/").append(uid);
    return HttpClientUtil.doGet(sb.toString());
  }

  //用户收藏模板
  @PostMapping("/usr/add")
  private String addUsrTemp(@RequestBody  JSONObject o) {
    StringBuilder sb = new StringBuilder();
    sb.append(dwUrl).append("/api/usrtemplete/commit");
    return HttpClientUtil.doPost(sb.toString(), o);
  }

  //删除收藏的模板
  @PostMapping("/usr/del/{id}")
  private String delUsrTemp(@PathVariable String id) {
    StringBuilder sb = new StringBuilder();
    sb.append(dwUrl).append("/api/usrtemplete/del/").append(id);
    return HttpClientUtil.doGet(sb.toString());
  }

  //更新收藏的模板
  @PostMapping("/usr/update")
  private Object updateUsrTemp(@RequestBody JSONObject o) {
    if (null==o||null == o.get("id")){
      return ResultView.error(ResultEnum.CODE_2);
    }
    StringBuilder sb = new StringBuilder();
    sb.append(dwUrl).append("/api/usrtemplete/update/").append(o.get("id"));
    return HttpClientUtil.doPost(sb.toString(), o);
  }

  //获取用户模板详情
  @PostMapping("/usr/detail/{templeteId}")
  private String getUsrTemp(@PathVariable String templeteId) {
    StringBuilder sb = new StringBuilder();
    sb.append(dwUrl).append("/api/usrtemplete/").append(templeteId);
    return HttpClientUtil.doGet(sb.toString());
  }
}
